<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px" @submit.native.prevent>
          <el-form-item label="类型名称" prop="typeName">
            <el-input
              v-model="queryParams.typeName"
              placeholder="请输入类型名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型编码" prop="typeCode">
            <el-input
              v-model="queryParams.typeCode"
              placeholder="请输入类型编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <div>
          <div class="flex-1" />
          <div style="display:flex;">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <dt-table
        :first-table-col="firstTableCol"
        :option-row="optionRow"
        :table-data="MessageTypeList"
        :table-col="tableCol"
        :row-key="rowKey"
        :operator="operator"
        :operator-col="{width:198}"
        :pagination-data="{
          total:total,
          queryParams:queryParams
        }"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改应用类型对话框 -->
    <dt-dialog :title="title" :visible.sync="open" :is-loading="loading" @comfirmBtn="submitForm" @closeDialog="cancel">
      <div slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="82px">
          <el-form-item label="类型名称" prop="typeName">
            <el-input v-model.trim="form.typeName" placeholder="请输入类型名称" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="类型编码" prop="typeCode">
            <el-input v-model.trim="form.typeCode" placeholder="请输入类型编码" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model.trim="form.remark" type="textarea" placeholder="请输入描述" maxlength="30" show-word-limit class="limit" />
          </el-form-item>
        </el-form>
      </div>
    </dt-dialog>
  </div>
</template>

<script>
import { listMessageType, getMessageType, delMessageType, addMessageType, updateMessageType } from '@/framework/api/applicationType/applicationType'
export default {
  name: 'MessageType',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显隐表格
      isShowTable: true,
      // 选中数组
      ids: [],
      // 列显隐数组
      showColumns: [
        { prop: 'typeName', label: '类型名称', show: true },
        { prop: 'typeCode', label: '类型编码', show: true },
        { prop: 'remark', label: '描述', show: true },
        { prop: 'createTime', label: '创建时间', show: true },
        { prop: 'updateTime', label: '修改时间', show: true }
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用类型表格数据
      MessageTypeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        typeName: null,
        typeCode: null
      },
      firstTableCol: {
        select: true
      },
      optionRow: {
        addBtn: {
          isShow: true,
          permission: ['APPLICATIONTYPE_ADD_BUTTON'],
          type: 'primary',
          disabled: false,
          icon: 'el-icon-plus',
          addClick: () => {
            this.handleAdd()
          }
        }
      },
      rowKey: 'typeId',
      tableCol: [
        {
          prop: 'typeName',
          label: '类型名称',
          show: true
        },
        {
          prop: 'typeCode',
          label: '类型编码',
          show: true
        },
        {
          prop: 'remark',
          label: '描述',
          show: true
        },
        {
          prop: 'createTime',
          label: '创建时间',
          show: true
        },
        {
          prop: 'updateTime',
          label: '修改时间',
          show: true
        }
      ],
      operator: [
        { 'text': '修改', permission: ['APPLICATIONTYPE_UPDATE_BUTTON'], icon: 'el-icon-edit', click: (row, col, cellValue) => { return this.handleUpdate(row) } },
        { 'text': '删除', permission: ['APPLICATIONTYPE_DEL_BUTTON'], icon: 'el-icon-delete', type: 'danger', click: (row, col, cellValue) => { return this.handleDelete(row) } }

      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: (rule, value, callback) => {
            if (!this.validate.checkAccountName(value)) {
              callback(new Error('类型名称格式错误'))
            } else callback()
          }
          }
        ],
        typeCode: [
          { required: true, message: '类型编码不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: (rule, value, callback) => {
            if (!this.validate.checkSocialCode(value)) {
              callback(new Error('类型编码格式错误'))
            } else callback()
          }
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询应用类型列表 */
    getList() {
      this.loading = true
      listMessageType(this.queryParams).then(({ data: response }) => {
        this.MessageTypeList = response.rows
        this.total = response.totalRows
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: null,
        typeName: null,
        typeCode: null,
        describe: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加应用类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const typeId = row.typeId || this.ids
      getMessageType(typeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改应用类型'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.typeId != null) {
            updateMessageType(this.form).then(response => {
              this.$dtModal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMessageType(this.form).then(response => {
              this.$dtModal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeIds = row.typeId || this.ids
      this.$dtModal.confirm('是否确认删除应用类型编号为"' + typeIds + '"的数据项？').then(function() {
        return delMessageType(typeIds)
      }).then(() => {
        this.getList()
        this.$dtModal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>
